package com.ybear.ybutils.utils.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;

import com.ybear.ybutils.utils.ResUtil;

import java.util.Arrays;

public class TextStyle {
    private final DialogInit init = DialogInit.get();
    private int lpWidth = -2, lpHeight = -2, width = -1, height = -1;
    @ColorInt
    private int backgroundColor = init.getDefTextBackgroundColor();
    private Drawable background = init.getDefTextBackground();
    @DrawableRes
    private int backgroundRes = init.getDefTextBackgroundRes();
    private int textUnit = init.getDefTextUnit();
    private float textSize = init.getDefTextSize();
    @ColorRes
    private int textColor = init.getDefTextColor();
    @ColorInt
    private int textColorInt = init.getDefTextColorInt();
    private Typeface textTypeface = init.getDefTextTypeface();
    private int style = init.getDefTextStyle();
    private int gravity = init.getDefTextGravity();
    private int[] paddings = init.getDefTextPaddings();
    private int maxLines = init.getDefTextMaxLines();
    private boolean enableHorizontalScroll;
    private boolean enableVerticalScroll;
    private View.OnClickListener onClickListener;
    private View.OnLongClickListener onLongClickListener;

    @Override
    public String toString() {
        return "TextStyle{" +
                "lpWidth=" + lpWidth +
                ", lpHeight=" + lpHeight +
                ", width=" + width +
                ", height=" + height +
                ", backgroundColor=" + backgroundColor +
                ", background=" + background +
                ", backgroundRes=" + backgroundRes +
                ", textUnit=" + textUnit +
                ", textSize=" + textSize +
                ", textColor=" + textColor +
                ", textColorInt=" + textColorInt +
                ", textTypeface=" + textTypeface +
                ", style=" + style +
                ", gravity=" + gravity +
                ", paddings=" + Arrays.toString(paddings) +
                ", maxLines=" + maxLines +
                ", enableHorizontalScroll=" + enableHorizontalScroll +
                ", enableVerticalScroll=" + enableVerticalScroll +
                ", onClickListener=" + onClickListener +
                ", onLongClickListener=" + onLongClickListener +
                '}';
    }

    public static TextStyle get() { return new TextStyle(); }

    public TextStyle setLayoutParams(int width, int height) {
        lpWidth = width;
        lpHeight = height;
        return this;
    }

    public TextStyle setSize(int width, int height) {
        this.width = width;
        this.height = height;
        return this;
    }

    public int getBackgroundColor() { return backgroundColor; }
    public TextStyle setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }

    public Drawable getBackground() { return background; }
    public TextStyle setBackground(Drawable background) {
        this.background = background;
        return this;
    }

    public int getBackgroundRes() { return backgroundRes; }
    public TextStyle setBackgroundRes(int backgroundRes) {
        this.backgroundRes = backgroundRes;
        return this;
    }

    public int getTextUnit() { return textUnit; }
    public TextStyle setTextUnit(int textUnit) {
        this.textUnit = textUnit;
        return this;
    }

    public float getTextSize() { return textSize; }
    public TextStyle setTextSize(float textSize) {
        this.textSize = textSize;
        return this;
    }

    @ColorRes
    public int getTextColor() { return textColor; }
    public TextStyle setTextColor(@ColorRes int textColor) {
        this.textColor = textColor;
        return this;
    }

    @ColorInt
    public int getTextColorInt() { return textColorInt; }
    public TextStyle setTextColorInt(int textColorInt) {
        this.textColorInt = textColorInt;
        return this;
    }

    public Typeface getTextTypeface() { return textTypeface; }
    /**
     *
     * @param textTypeface  {@link Typeface}
     * @param style         {@link Typeface#NORMAL}
     *                      {@link Typeface#BOLD}
     *                      {@link Typeface#ITALIC}
     *                      {@link Typeface#BOLD_ITALIC}
     * @return              {@link TextStyle}
     */
    public TextStyle setTextTypeface(Typeface textTypeface, int style) {
        this.textTypeface = textTypeface;
        this.style = style;
        return this;
    }
    public TextStyle setTextTypeface(Typeface textTypeface) {
        this.textTypeface = textTypeface;
        return this;
    }

    public int getStyle() { return style; }

    public int getGravity() { return gravity; }
    public TextStyle setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public TextStyle setPadding(int left, int top, int right, int bottom) {
        paddings = new int[] { left, top, right, bottom };
        return this;
    }
    public TextStyle setPadding(int p) { return setPadding(p, p, p, p); }

    public int getMaxLines() { return maxLines; }
    public TextStyle setMaxLines(int maxLines) {
        this.maxLines = maxLines;
        return this;
    }

    public boolean isEnableHorizontalScroll() { return enableHorizontalScroll; }
    public TextStyle setEnableHorizontalScroll(boolean enableHorizontalScroll) {
        this.enableHorizontalScroll = enableHorizontalScroll;
        return this;
    }

    public boolean isEnableVerticalScroll() { return enableVerticalScroll; }
    public TextStyle setEnableVerticalScroll(boolean enableVerticalScroll) {
        this.enableVerticalScroll = enableVerticalScroll;
        return this;
    }

    public TextStyle setOnClickListener(View.OnClickListener l) {
        onClickListener = l;
        return this;
    }
    public TextStyle setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
        return this;
    }

    public TextView getTextView(Context context, String text) {
        return getTextView( context, new TextView( context ), text );
    }
    public TextView getTextView(Context context, TextView v, String text) {
        v.setText( text );
        v.setTextSize( textUnit, textSize );
        v.setTextColor( textColorInt == -1 ?
                ResUtil.getColor( context, textColor ) :
                textColorInt
        );

        //Background
        if( backgroundRes != 0 ) {
            v.setBackgroundResource( backgroundRes );       //背景资源
        }else if( backgroundColor != 0 ) {
            v.setBackgroundColor( backgroundColor );        //背景颜色
        } else if( background != null ) {
            //背景图
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                v.setBackground( background );
            }else {
                v.setBackgroundDrawable( background );
            }
        }else {
            v.setBackgroundColor( Color.TRANSPARENT );      //默认透明
        }

        //Typeface
        if( textTypeface != null ) {
            if( style == 0 ) {
                v.setTypeface( textTypeface );
            }else {
                v.setTypeface( textTypeface, style );
            }
        }

        v.setGravity( gravity );
        if( maxLines != -1 ) v.setMaxLines( maxLines );

        if( paddings != null ) v.setPadding( paddings[0], paddings[1], paddings[2], paddings[3] );

        if( enableHorizontalScroll || enableVerticalScroll ) {
            v.setMovementMethod(ScrollingMovementMethod.getInstance());
            v.setHorizontalScrollBarEnabled( enableHorizontalScroll );
            v.setVerticalScrollBarEnabled( enableVerticalScroll );
        }

        //点击/长按事件
        if( onClickListener != null || onLongClickListener != null ) {
            v.setFocusable( true );
            if( onClickListener != null ) {
                v.setClickable( true );
                v.setOnClickListener( onClickListener );
            }
            if( onLongClickListener != null ) {
                v.setLongClickable( true );
                v.setOnLongClickListener( onLongClickListener );
            }
        }
        //显示
        if( v.getVisibility() != View.VISIBLE ) v.setVisibility( View.VISIBLE );
        return v;
    }
    public TextView createView(Context context, String text) {
        TextView v = getTextView(context, text);
        v.setLayoutParams( new ViewGroup.LayoutParams(lpWidth, lpHeight) );
        if( width != -1 ) v.setWidth( width );
        if( height != -1 ) v.setHeight( height );
        return v;
    }
}